<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		Broadcastweb.eu - Paules												/
/	Site:			http://www.broadcastweb.eu												/
/	Email:			support@broadcastweb.eu													/
/	Copyright:		2011 - 2012 (c) Broadcastweb.eu											/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0003																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla security
defined('_JEXEC') or die;
$samdb = $this->samdb;
$songtype = $this->settings->request->songtype;
function getTopReq($samdb, $songtype){
	$query = "
		SELECT ID, artist, title, count_requested
		FROM songlist
		WHERE songtype IN (".$songtype.")
		ORDER BY count_requested DESC, count_played DESC, artist DESC
		LIMIT 0, 5 ";
	$result = $samdb->getObjectList($query);
	return $result;
}

//============ Document build ============//
echo '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
echo '<joosam>'."\n";

//== Event
echo "\t".'<event>'."\n";

$mod_event_title = $this->settings->event->module_title;
//$mod_event_title = '';
$mod_event_content = $this->settings->event->module_content;
if($mod_event_title == ''){ $mod_event_title = JText::_('COM_JOOSAM_AJAX_MODEVENT_TITLE');}
if($mod_event_content == ''){$mod_event_content = JText::_('COM_JOOSAM_AJAX_MODEVENT_CONTENT');}
echo "\t\t".'<title>'.htmlspecialchars(utf8_encode($mod_event_title)).'</title>'."\n";
echo "\t\t".'<content>'.htmlspecialchars(utf8_encode($mod_event_content)).'</content>'."\n";

echo "\t".'</event>'."\n";

//== TopRequest
echo "\t".'<topdata>'."\n";

$songdata = getTopReq($samdb, $songtype);
if(is_array($songdata)){
	foreach($songdata as $i => $song){
		if($song->artist == ''){ $song->artist = 'jsm_false';}
		if($song->title == ''){ $song->title = 'jsm_false';}
		echo "\t\t".'<song num="'.$i.'">'."\n";
		echo "\t\t\t".'<songid>'.htmlspecialchars(utf8_encode($song->ID)).'</songid>'."\n";
		echo "\t\t\t".'<artist>'.htmlspecialchars(utf8_encode($song->artist)).'</artist>'."\n";
		echo "\t\t\t".'<title>'.htmlspecialchars(utf8_encode($song->title)).'</title>'."\n";
		echo "\t\t\t".'<requested>'.htmlspecialchars(utf8_encode($song->count_requested)).'</requested>'."\n";
		echo "\t\t".'</song>'."\n";
	}
}

echo "\t".'</topdata>'."\n";


//== LiveModule
echo "\t".'<livedata>'."\n";
$liveMod = $this->getdata->jsm_curplayed();
if($liveMod){
	$played = strtotime($liveMod->played);
	$duration = round($liveMod->duration/1000);
	$songEnd = date('Y-m-d H:i:s', mktime(date("H", $played), date("i", $played), date("s", $played)+$duration, date("m", $played), date("d", $played), date("Y", $played)));
	$timeleft = strtotime($songEnd) - strtotime($liveMod->datetime);
	if($liveMod->songid == ''){ $liveMod->songid = 'jsm_false';}
	if($liveMod->artist == ''){ $liveMod->artist = 'jsm_false';}
	if($liveMod->title == ''){ $liveMod->title = 'jsm_false';}
	if($timeleft == ''){ $timeleft = 'jsm_false';}
	if($liveMod->image == ''){ $liveMod->image = 'jsm_false';}
	if($liveMod->request == ''){ $liveMod->request = 'jsm_false';}
	if($liveMod->request_name == ''){ $liveMod->request_name = 'jsm_false';}
	if($liveMod->request_msg == ''){ $liveMod->request_msg = 'jsm_false';}
	echo "\t\t".'<songid>'.htmlspecialchars($liveMod->songid).'</songid>'."\n";
	echo "\t\t".'<artist>'.htmlspecialchars($liveMod->artist).'</artist>'."\n";
	echo "\t\t".'<title>'.htmlspecialchars($liveMod->title).'</title>'."\n";
	echo "\t\t".'<duration>'.htmlspecialchars($timeleft).'</duration>'."\n";
	echo "\t\t".'<picture>'.htmlspecialchars($liveMod->image).'</picture>'."\n";
	echo "\t\t".'<requestid>'.htmlspecialchars($liveMod->request).'</requestid>'."\n";
	echo "\t\t".'<requestname>'.htmlspecialchars($liveMod->request_name).'</requestname>'."\n";
	echo "\t\t".'<requestmsg>'.htmlspecialchars($liveMod->request_msg).'</requestmsg>'."\n";
	
	// Manual request
	// echo "\t\t".'<requestid>110</requestid>'."\n";
	// echo "\t\t".'<requestname>Req Name</requestname>'."\n";
	// echo "\t\t".'<requestmsg>JooSam request message text</requestmsg>'."\n";
} else{
	echo "\t\t".'<songid>jsm_false</songid>'."\n";
	echo "\t\t".'<artist>jsm_false</artist>'."\n";
	echo "\t\t".'<title>jsm_false</title>'."\n";
	echo "\t\t".'<duration>jsm_false</duration>'."\n";
	echo "\t\t".'<picture>jsm_false</picture>'."\n";
	echo "\t\t".'<requestid>jsm_false</requestid>'."\n";
	echo "\t\t".'<requestname>jsm_false</requestname>'."\n";
	echo "\t\t".'<requestmsg>jsm_false</requestmsg>'."\n";
}
echo "\t".'</livedata>'."\n";

//== History Module
echo "\t".'<historydata>'."\n";
$history = $this->getdata->jsm_history();
if(is_array($history)){
	foreach($history as $i => $song){
		if($song->artist == ''){ $song->artist = 'jsm_false';}
		if($song->title == ''){ $song->title = 'jsm_false';}
		echo "\t\t".'<song num="'.$i.'">'."\n";
		echo "\t\t\t".'<songid>'.htmlspecialchars(utf8_encode($song->songid)).'</songid>'."\n";
		echo "\t\t\t".'<artist>'.htmlspecialchars($song->artist).'</artist>'."\n";
		echo "\t\t\t".'<title>'.htmlspecialchars($song->title).'</title>'."\n";
		echo "\t\t\t".'<played>'.htmlspecialchars($song->played).'</played>'."\n";
		echo "\t\t".'</song>'."\n";
	}
}
echo "\t".'</historydata>'."\n";

//== Queue mod jsm_queue
echo "\t".'<queue>'."\n";
$queue = $this->getdata->jsm_queue();
if(is_array($queue)){
	foreach($queue as $i => $song){
		if($song->artist == ''){ $song->artist = 'jsm_false';}
		if($song->title == ''){ $song->title = 'jsm_false';}
		echo "\t\t".'<song num="'.$i.'">'."\n";
		echo "\t\t\t".'<songid>'.htmlspecialchars(utf8_encode($song->songid)).'</songid>'."\n";
		echo "\t\t\t".'<artist>'.htmlspecialchars($song->artist).'</artist>'."\n";
		echo "\t\t\t".'<title>'.htmlspecialchars($song->title).'</title>'."\n";
		echo "\t\t".'</song>'."\n";
	}
}
echo "\t".'</queue>'."\n";

//== Close document
echo '</joosam>'."\n";